<?php
namespace Admin\Controller;
use Think\Controller;

class ArticleController extends PublicController
{

    //文章管理显示
    public function index()
    {
        // $data = D('Article')->relation(true)->select();
        // v($data);exit;
        // $this->assign('data', $data);
        $this->page();
        // $this->display('Article/index');
    }

    //文章发布状态
    // public function status()
    // {
    //     // 判断 有无传递ID
    //     if (empty($_GET['id'])) {
    //         $this->redirect('Article/index');
    //         exit;
    //     }
        
    //     $id = I('get.id/d');
    //     switch ($_GET['action']) {
    //         case 'pz':
    //             $status = '1';
    //             $where['id'] = $id;
    //             $save['status'] = $status;
    //             M('Article')->where($where)->save($save);
    //             $this->redirect('Article/index');
    //             break;
    //         case 'fj':
    //             $status = '2';
    //             $where['id'] = $id;
    //             $save['status'] = $status;
    //             M('Article')->where($where)->save($save);
    //             $this->redirect('Article/index');
    //             break;
    //     }
    // }

    //模态框显示
    public function ajax()
    {
        $id = I('post.id/d');
        $where['id'] = $id;
        $data = M('article')->where($where)->getField('content');
        $this->ajaxReturn($data);
    }

    public function page()
    {

        $_GET['p'] = empty($_GET['p'])?'1':$_GET['p'];
        
        if ($_POST['title']) {
            $title = $_POST['title'];
        } elseif ($_GET['title']) {
            $title = $_GET['title'];
        }
        
        $where['title'] = array('like', '%'.$title.'%');
        $Article = D('Article'); // 实例化Article对象
        
        // 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
        $list = $Article->field('c.cname, u.name, a.id, a.title, a.create_time, a.zan, a.content, a.read_times, a.status')
                ->table('hx_article a, hx_user u, hx_category c')
                ->order('id desc')
                ->page($_GET['p'].',1')
                ->where('c.id=a.cate_id AND a.user_id=u.id')
                ->where($where)->select();
        // v($list);exit;
        $this->assign('data',$list);// 赋值数据集
        $count = $Article->table('hx_article a, hx_user u, hx_category c')
                ->where('c.id=a.cate_id AND a.user_id=u.id')
                ->where($where)->count();// 查询满足要求的总记录数
        // v($count);exit;
        $Page = new \Think\Page($count,1);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->parameter["title"] = $title;
        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->display('Article/index'); // 输出模板
    }

    //pz AJAX
    public function pzAjax()
    {
        $id=$_GET['id'];
        $where['id'] = $id;
        $save['status'] = 1;
        if (M('article')->where($where)->save($save)) {
            $this->ajaxReturn(1);
        } else {
            $this->ajaxReturn(0);
        }
    }

    //fj AJAX
    public function fjAjax()
    {
        $id=$_GET['id'];
        $where['id'] = $id;
        $save['status'] = 2;
        if (M('article')->where($where)->save($save)) {
            $this->ajaxReturn(1);
        } else {
            $this->ajaxReturn(0);
        }
    }
}
